<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书管理</title>
<!-- 引入css样式文件 -->
<!-- Bootstrap Core CSS -->
<link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet" />
<!-- MetisMenu CSS -->
<link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet" />
<!-- DataTables CSS -->
<link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet" />
<!-- Custom CSS -->
<link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet" />
<!-- Custom Fonts -->
<link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet"
	type="text/css" />
<link href="<%=basePath%>css/boot-crm.css" rel="stylesheet"
	type="text/css" />
</head>
<body>
	<div id="wrapper">
		<!-- 导航栏部分 -->
		<nav class="navbar navbar-default navbar-static-top" role="navigation"
			style="margin-bottom: 0">

			<div class="navbar-header">
				<a class="navbar-brand" href="<%=basePath%>book/list.action">booksshop子系统
					-图书管理 </a>
			</div>
		</nav>
		<!-- 图书列表查询部分  start-->
		<div id="page-wrapper">
			<div class="row">
				<div class="col-lg-12">
					<h1 class="page-header">图书管理</h1>
				</div>
				<!-- /.col-lg-12 -->
			</div>
			<!-- /.row -->
			<div class="panel panel-default">
				<div class="panel-body">
					<form class="form-inline" method="get"
						action="${pageContext.request.contextPath }/book/list.action">
						<div class="form-group">
							<label for="bookName">图书名称</label> <input type="text"
								class="form-control" id="bookName" value="${bookName }"
								name="bookName" />
						</div>
						<!-- 
						<div class="form-group">
							<label for="bookFrom">图书来源</label> <select
								class="form-control" id="bookFrom" name="custSource">
								<option value="">--请选择--</option>
								<c:forEach items="${fromType}" var="item">
									<option value="${item.dictId}"
										<c:if test="${item.dictId == custSource}">selected</c:if>>
										${item.dictItemName }</option>
								</c:forEach>
							</select>
						</div>
						 -->
						<button type="submit" class="btn btn-primary">查询</button>
					</form>
				</div>
			</div>

			<!-- ------------------------------------------------------------------------------------------------------------------------------ -->


			<a href="#" class="btn btn-primary" data-toggle="modal"
				data-target="#newbookDialog" onclick="clearbook()">新建</a>
			<!-- ------------------------------------------------------------------------------------------------------------------------------ -->
			<div class="row">
				<div class="col-lg-12">
					<div class="panel panel-default">
						<div class="panel-heading">图书信息列表</div>
						<!-- /.panel-heading -->
						<table class="table table-bordered table-striped">
							<thead>
								<tr>
									<th>编号</th>
									<th>图书名称</th>
									<th>作者</th>
									<th>出版社</th>
									<th>价格</th>
									<th>是否删除</th>
								</tr>
							</thead>
							<tbody>
								<c:forEach items="${page.rows}" var="row">
									<tr>
										<!-- &{}中的内容是bookMapper.xml中的resultMap中的property -->
										<td>${row.bookid}</td>
										<td>${row.title}</td>
										<td>${row.author}</td>
										<td>${row.publisher}</td>
										<td>${row.price}</td>
										<td>${row.isdeleted ? '是' : '否'}</td>
										<td><a href="#" class="btn btn-primary btn-xs"
											data-toggle="modal" data-target="#bookEditDialog"
											onclick="editbook(${row.bookid})">修改</a> <a href="#"
											class="btn btn-danger btn-xs"
											onclick="deletebook(${row.bookid})">删除</a></td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
						<div class="col-md-12 text-right">
							<ujn:page
								url="${pageContext.request.contextPath }/book/list.action" />
						</div>
						<!-- /.panel-body -->
					</div>
					<!-- /.panel -->
				</div>
				<!-- /.col-lg-12 -->
			</div>
		</div>
		<!-- 图书列表查询部分  end-->
	</div>

	<!-- ################################################################################################# -->
	<!-- 新建图书模态框 -->
	<div class="modal fade" id="newbookDialog" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">新建图书信息</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" id="new_book_form">
						<div class="form-group">
							<label for="new_title" class="col-sm-2 control-label">
								图书名称 </label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="new_title"
									placeholder="图书名称" name="title" />
							</div>
						</div>

						<div class="form-group">
							<label for="new_author" class="col-sm-2 control-label">作者</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" id="new_author"
									placeholder="作者" name="author" />
							</div>
						</div>
						<div class="form-group">
							<label for="new_publisher" class="col-sm-2 control-label">出版社</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="new_publisher"
									placeholder="出版社" name="publisher" />
							</div>
						</div>
						<div class="form-group">
							<label for="new_price" class="col-sm-2 control-label">价格</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" id="new_price"
									placeholder="价格" name="price" />
							</div>
						</div>

						<div class="form-group">
							<label for="edit_isdeleted" class="col-sm-2 control-label">是否删除</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_isdeleted"
									placeholder="是否删除" name="isdeleted" />
							</div>
						</div>

					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary"
						onclick="createbook()">创建图书</button>
				</div>
			</div>
		</div>
	</div>
	<!-- ################################################################################################# -->
	<!-- 修改图书模态框 -->
	<div class="modal fade" id="bookEditDialog" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">修改图书信息</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" id="edit_book_form">

						<input type="hidden" id="bookid" name="bookid" />
						<div class="form-group">
							<label for="edit_title" class="col-sm-2 control-label">图书名称
							</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_title"
									placeholder="图书名称" name="title" />
							</div>
						</div>

						<div class="form-group">
							<label for="edit_author" class="col-sm-2 control-label">作者</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_author"
									placeholder="作者" name="author" />
							</div>
						</div>
						<div class="form-group">
							<label for="edit_publisher" class="col-sm-2 control-label">出版社</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_publisher"
									placeholder="出版社" name="publisher" />
							</div>
						</div>
						<div class="form-group">
							<label for="edit_price" class="col-sm-2 control-label">价格</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" id="edit_price"
									placeholder="价格" name="price" />
							</div>
						</div>

						<div class="form-group">
							<label for="edit_isdeleted" class="col-sm-2 control-label">是否删除</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_isdeleted"
									placeholder="true or false" name="isdeleted" />
							</div>
						</div>

					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary"
						onclick="updatebook()">保存修改</button>
				</div>
			</div>
		</div>
	</div>
	<!-- ################################################################################################# -->
	<!-- 引入js文件 -->
	<!-- jQuery -->
	<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
	<!-- Bootstrap Core JavaScript -->
	<script src="<%=basePath%>js/bootstrap.min.js"></script>
	<!-- Metis Menu Plugin JavaScript -->
	<script src="<%=basePath%>js/metisMenu.min.js"></script>
	<!-- DataTables JavaScript -->
	<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
	<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
	<!-- Custom Theme JavaScript -->
	<script src="<%=basePath%>js/sb-admin-2.js"></script>
	<!-- 编写js代码 -->
	<script type="text/javascript">
	var data="${data}";
//清空新建图书窗口中的数据
	function clearbook() {
	    $("#new_bookName").val("");
	    $("#new_password").val("")
	    $("#new_email").val("")
	    $("#new_address").val("")
	    $("#new_isdeleted").val("");
	}
	// 创建图书
	function createbook() {
	$.post("<%=basePath%>book/create.action",
	$("#new_book_form").serialize(),function(data){
	        if(data =="OK"){
	            alert("图书创建成功！");
	            window.location.reload();
	        }else{
	            alert("图书创建失败！");
	            window.location.reload();
	        }
	    });
	}
	// 通过id获取修改的图书信息
	// document.getElementById("bookid") 引号中的内容是表单中的id
	function editbook(id) {
	    $.ajax({
	        type:"get",
	        url:"<%=basePath%>book/getBookById.action",
	        data:{"id":id},
	        success:function(data) {
	        	$("#bookid").val(data.bookid);
	            $("#edit_title").val(data.title);
	            $("#edit_author").val(data.author);
	            $("#edit_publisher").val(data.publisher);
	            $("#edit_price").val(data.price);
	            $("#edit_isdeleted").val(data.isdeleted);
	        }
	    });
	}
    // 执行修改图书操作
	function updatebook() {
		$.post("<%=basePath%>book/update.action",$("#edit_book_form").serialize(),function(data){
			if(data =="OK"){
				alert("图书信息更新成功！");	
				window.location.reload();
			}else{
				alert("图书信息更新失败！");
				window.location.reload();
			}
		});
	}
	// 删除图书
	function deletebook(id) {
	    if(confirm('确实要删除该图书吗?')) {
	$.post("<%=basePath%>book/delete.action",{"id":id,"data": data},
	function(data){
	            if(data =="OK"){
	                alert("图书删除成功！");
	                window.location.reload();
	            }else{
	                alert("删除图书失败！");
	                window.location.reload();
	                
	            }
	        });
	    }
	}
</script>
</body>
</html>